package com.base.prefixsum;

/**
 * @ClassName: CorpFlightBookings
 * @Description: 1109. 航班预订统计
 * @author: li
 * @Date: 2021/8/31 6:53 下午
 */
public class CorpFlightBookings {
    /**
     * 暴力法
     * @param bookings
     * @param n
     * @return
     */
    public int[] corpFlightBookings(int[][] bookings, int n) {
        int[] sum = new int[n];
        for (int[] booking : bookings) {
            for (int j = booking[0]; j <= booking[1]; j++) {
                sum[j - 1] += booking[2];
            }
        }
        return sum;
    }

    /**
     * 差分法
     * @param bookings
     * @param n
     * @return
     */
    public int[] corpFlightBookings1(int[][] bookings,int n){
        int[] nums = new int[n];
        for (int[] booking: bookings) {
            nums[booking[0]-1] += booking[2];
            if (booking[1]<n){
                nums[booking[1]] -= booking[2];
            }
        }
        for (int i = 1; i < nums.length ; i++) {
            nums[i]+= nums[i-1];
        }
        return nums;
    }
}
